#include "contact.h"
#include <stdio.h>
#include <strings.h>
#include <string.h>

/* 全局变量 */
extern struct ContactBook books;


void displayInterface()
{
    printf("1.新建联系人\n");
    printf("2.删除联系人\n");
    printf("3.修改联系人\n");
    printf("4.查看联系人\n");
}


int contactInit()
{
    FILE * fp = fopen("./contact.txt", "r");
    if (fp == NULL)
    {
        perror("open error");
        return -1;
    }

    char *buffer = NULL;
    size_t size = 0;
    
    int readBytes = 0;
    while ((readBytes = getline(&buffer, &size, fp)))
    {    
        if (feof(fp) == 1)
        {
            break;
        }

        strncpy(books.perBooks[books.size].name, buffer, sizeof(books.perBooks[books.size].name));
        strncpy(books.perBooks[books.size].phone, buffer + 20, sizeof(books.perBooks[books.size].phone));

        (books.size)++;
    }

}

int createContact()
{
    struct Person per;
    bzero(&per, sizeof(per));

    printf("please input name:");
    scanf("%s", per.name);
    printf("please input phone:");
    scanf("%s", per.phone);

    books.perBooks[(books.size)++] = per;

    /* 打开文件 */
    FILE * fp = fopen("./contact.txt", "a");
    if (fp == NULL)
    {
        perror("open error");
        return -1;
    }

    /* 写文件 */
    fwrite(per.name, 1, sizeof(per.name), fp);
    fwrite(per.phone, 1, sizeof(per.phone), fp);

    char * line = "\n";
    fwrite(line, 1, 1, fp);
    
    /* 关闭文件 */
    fclose(fp);

    return 0;
}

int searchPerson(char *name)
{
    int cmp = 0;
    for (int idx = 0; idx < books.size; idx++)
    {
        cmp = strcmp(books.perBooks[idx].name, name);
        if (cmp == 0)
        {
            return idx;
        }
        
    }
   return -1;
}

int deleteContact(char*name)
{  
    struct Person per;
    bzero(&per, sizeof(per));

    printf("please input name:");
    scanf("%s", per.name);
   int cmp = 0;
    for (int idx = 0; idx < books.size; idx++)
    {
        cmp = strcmp(books.perBooks[idx].name, name);
        if (cmp == 0)
        {
            return idx;
        }
        
    }
   return -1;
   /* 打开文件 */
    FILE * fp = fopen("./contact.txt", "a");
    if (fp == NULL)
    {
        perror("open error");
        return -1;
    }


}

int modifyContact()
{
    struct Person per;
    bzero(&per, sizeof(per));

    printf("please input name:");
    scanf("%s", per.name);
   int cmp = 0;
    for (int idx = 0; idx < books.size; idx++)
    {
        cmp = strcmp(books.perBooks[idx].name, name);
        if (cmp == 0)
        {
            return idx;
        }
        
    }
   return -1;
   /* 打开文件 */
    FILE * fp = fopen("./contact.txt", "a");
    if (fp == NULL)
    {
        perror("open error");
        return -1;
    }

}

int showContact()
{
    FILE * fp = fopen("./contact.txt", "r");
    if (fp == NULL)
    {
        perror("open error:");
        return -1;
    }
    char *buffer = NULL;
    size_t size = 0;
    
    int readBytes = 0;
    while ((readBytes = getline(&buffer, &size, fp)))
    {    
        if (feof(fp) == 1)
        {
            break;
        }

        char name[20] = { 0 };
        char phone[12] = { 0 };

        strncpy(name, buffer, sizeof(name));
        strncpy(phone, buffer + 20, sizeof(phone));

        printf("name : %s\t phone = %s\n", name, phone);
    }
}